package com.hyy.demo.sample3;

import com.alibaba.fastjson.JSON;
import org.squirrelframework.foundation.fsm.*;

import java.util.Arrays;
import java.util.List;

/**
 * Created by hyygavin on 2017/9/29.
 */
public class QuickStartSample {

    public static void main(String[] args) {
        UntypedStateMachineBuilder builder = StateMachineBuilderFactory.create(RuleMachine.class);

        List<String> emotions = Arrays.asList("emotion1","emotion2","emotion3");
        for (String emotion : emotions) {
            builder.localTransition().from("emotion").to(emotion).on("emotion,"+emotion);
            builder.localTransition().from(emotion).to("emotion").on(emotion+",emotion");
            builder.onEntry(emotion).perform(new EmotionAction(emotion));
        }

        builder.externalTransition().from("emotion").to("").on("emotion,");
        UntypedStateMachine fsm = builder.newStateMachine("emotion");
        fsm.start();
        fsm.fire("emotion,emotion1");
        System.out.println("fsm==="+fsm.getCurrentState());
        long time1 = System.currentTimeMillis();
        StateMachineData.Reader<UntypedStateMachine, Object, Object, Object> savedData = fsm.dumpSavedData();
        String savedDataStr = ObjectSerializableSupport.serialize(savedData);
        System.out.println(savedDataStr);
        StateMachineData.Reader<UntypedStateMachine, Object, Object, Object> loadedSavedData =
                ObjectSerializableSupport.deserialize(savedDataStr);
        UntypedStateMachine fsm1 = builder.newStateMachine("emotion");
        fsm1.loadSavedData(loadedSavedData);
        System.out.println("fsm1==="+fsm1.getCurrentState());
        long time2 = System.currentTimeMillis();
        System.out.println(time1-time2);
    }
}
